package com.kevin.effective;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @Description 3.使用私有构造方法或枚类实现Singleton属性
 * @Author Kevin
 * @Date: Created in  15:40 2019/12/12:
 * @Modified By:
 * @Email: tang.lx@topcheer.com
 */
public class Three {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        System.out.println(Singleton.getInstance()==Singleton.getInstance());
        Class<?> clazz = Class.forName("com.kevin.effective.Singleton");
        Constructor<?> constructor = clazz.getDeclaredConstructor();
        constructor.setAccessible(true);
        Singleton singleton1 = (Singleton) constructor.newInstance();
        Singleton singleton2 = (Singleton) constructor.newInstance();
        System.out.println(singleton1 == singleton2);
//        System.out.println(SingletonEnum.INSTANCE == SingletonEnum.INSTANCE);
    }
}

class Singleton {
    final static Singleton instance = new Singleton();
    private Singleton() {
    }
    public static Singleton getInstance(){
        return instance;
    }
}

enum SingletonEnum {
    INSTANCE;

    SingletonEnum() {
    }
}
